package com.usermanage.ExceptionConfig;

import com.usermanage.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.IOException;

/**
 * Created by 過客 on 2018/8/9
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class SpringExceptionAdvice {

    //运行时异常
//    @ExceptionHandler(RuntimeException.class)
//    public ModelAndView runtimeExceptionHandler(RuntimeException ex) {
//        return resultFormat(ex);
//    }
//
//    //空指针异常
//    @ExceptionHandler(NullPointerException.class)
//    public ModelAndView nullPointerExceptionHandler(NullPointerException ex) {
//        return resultFormat(ex);
//    }
//
//    //类型转换异常
//    @ExceptionHandler(ClassCastException.class)
//    public ModelAndView classCastExceptionHandler(ClassCastException ex) {
//        return resultFormat(ex);
//    }
//
//    //IO异常
//    @ExceptionHandler(IOException.class)
//    public ModelAndView iOExceptionHandler(IOException ex) {
//        return resultFormat(ex);
//    }
//
//    //未知方法异常
//    @ExceptionHandler(NoSuchMethodException.class)
//    public ModelAndView noSuchMethodExceptionHandler(NoSuchMethodException ex) {
//        return resultFormat(ex);
//    }
//
//    //数组越界异常
//    @ExceptionHandler(IndexOutOfBoundsException.class)
//    public ModelAndView indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
//        return resultFormat(ex);
//    }
//
//    //400错误
//    @ExceptionHandler({HttpMessageNotReadableException.class})
//    public ModelAndView requestNotReadable(HttpMessageNotReadableException ex) {
//        System.out.println("400..requestNotReadable");
//        return resultFormat(ex);
//    }
//
//    //400错误
//    @ExceptionHandler({TypeMismatchException.class})
//    public ModelAndView requestTypeMismatch(TypeMismatchException ex) {
//        System.out.println("400..TypeMismatchException");
//        return resultFormat(ex);
//    }
//
//    //400错误
//    @ExceptionHandler({MissingServletRequestParameterException.class})
//    public ModelAndView requestMissingServletRequest(MissingServletRequestParameterException ex) {
//        System.out.println("400..MissingServletRequest");
//        return resultFormat(ex);
//    }
//
//    //405错误
//    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
//    public ModelAndView request405(HttpRequestMethodNotSupportedException ex) {
//        return resultFormat(ex);
//    }
//
//    //406错误
//    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
//    public ModelAndView request406(HttpMediaTypeNotAcceptableException ex) {
//        System.out.println("406...");
//        return resultFormat(ex);
//    }
//
//    //500错误
//    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
//    public ModelAndView server500(RuntimeException ex) {
//        System.out.println("500...");
//        return resultFormat(ex);
//    }
//
//    //栈溢出
//    @ExceptionHandler({StackOverflowError.class})
//    public ModelAndView requestStackOverflow(StackOverflowError ex) {
//        return resultFormat(ex);
//    }

    //其他错误
    @ExceptionHandler({Exception.class})
    public ModelAndView exception(Exception ex) {
        return resultFormat(ex);
    }

    private <T extends Throwable> ModelAndView resultFormat(T ex) {
        ModelAndView mv;
//        String defaultMsg = "System error";  //默认异常信息
        String defaultMsg = ex.toString();  //临时打出错误信息方便调试
        log.error("异常信息内容："+defaultMsg);
        MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        JsonData result = JsonData.fail(defaultMsg);
//        JsonData result = JsonData.fail(ex.getMessage());
        mv = new ModelAndView(jsonView, result.errMap());
        return mv;
    }
}
